package com.mlink.ai.chat.ui.fragment

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.core.view.isVisible
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.mlink.ai.chat.assistant.robot.databinding.FragmentRecentBinding
import com.mlink.ai.chat.ui.activity.ChatActivity
import com.mlink.ai.chat.ui.activity.HistoryActivity
import com.mlink.ai.chat.ui.adapter.ChatHistoryRecyclerAdapter
import com.mlink.ai.chat.ui.viewmodel.RecentViewModel
import com.mlink.ai.chat.utils.AnalyticsEvents
import com.mlink.ai.chat.utils.logEvent

class RecentFragment : BaseFragment<FragmentRecentBinding>() {

    private lateinit var historyAdapter: ChatHistoryRecyclerAdapter
    private val viewModel: RecentViewModel by viewModels()
    private var recentDeleteView : View? = null

    override fun getViewBinding(): FragmentRecentBinding =
        FragmentRecentBinding.inflate(layoutInflater)


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initHistoryRecycler()

        viewModel.historyMessage.observe(viewLifecycleOwner) {
            historyAdapter.submitList(it)
            if (it.isEmpty()) {
                binding.historyRecycler.isVisible = false
                binding.groupNoHistory.isVisible = true
                return@observe
            }
            binding.historyRecycler.isVisible = true
            binding.groupNoHistory.isVisible = false
        }

        binding.tvStart.setOnClickListener {
            startActivity(
                Intent(requireActivity(), ChatActivity::class.java)
            )
        }

        viewModel.getAllHistoryMessage()
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!isHidden) {
            viewModel.getAllHistoryMessage()
        } else {
            recentDeleteView?.isVisible = false
        }
    }

    override fun onStop() {
        recentDeleteView?.isVisible = false
        recentDeleteView = null
        super.onStop()
    }

    override fun onResume() {
        super.onResume()
        // 延迟等待 chat 界面插入数据
        binding.historyRecycler.postDelayed({
            viewModel.getAllHistoryMessage()
        }, 500L)
    }

    private fun initHistoryRecycler() {
        historyAdapter = ChatHistoryRecyclerAdapter().apply {
            onMessageDelete = {
                viewModel.deleteHistoryMessage(it)
            }

            onMessageClick = {
                AnalyticsEvents.AC_RECENTS_HISTORY_CLICK.logEvent()

                val intent = Intent(requireActivity(), HistoryActivity::class.java).apply {
                    putExtra(HistoryActivity.KEY_HISTORY_DATA, it.messageContent)
                }
                startActivity(intent)
            }

            onDeleteShow = {
                recentDeleteView?.isVisible = false
                recentDeleteView = it
            }
        }
        binding.historyRecycler.run {
            adapter = historyAdapter
            layoutManager =
                LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
        }
    }
}